package com.zsj.juc.lock;

import java.util.concurrent.CountDownLatch;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/4/21 16:08
 * @description 闭锁
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        new CountDownLatchDemo().doMission(6);
    }

    public void doMission(int size) throws InterruptedException {
        if (size == 0) return;
        CountDownLatch countDownLatch = new CountDownLatch(size);
        for (int i = 0; i < size; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "处理任务");
                //计数器-1
                countDownLatch.countDown();
            }, "" + i).start();
        }
        //等待其它线程执行完成
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "收官");
    }
}
